package com.zy.socket.testfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author Gong
 * @Description 服务端向客户端发文件
 * @dATE 2021/12/19 14:44
 **/
public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            //获得客户端对象
            Socket socket = serverSocket.accept();
            System.out.println(socket);
            OutputStream outputStream = socket.getOutputStream();

            FileInputStream fileInputStream = new FileInputStream(new File("d:/temp/java.png"));
            //1.读图片
            byte[] buff = new byte[1024];
            int len;
            //2.发送图片
            while ((len=fileInputStream.read(buff)) > -1){
                outputStream.write(buff,0,len);
            }
            //告知客户端文件输出完毕
            socket.shutdownOutput();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
